package com.lesso.lzh.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.core.ApiResponse;
import com.lesso.lzh.sunflower.pojo.dto.RelEmployeeDeviceDTO;
import com.lesso.lzh.sunflower.pojo.dto.RelEmployeeDeviceQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.RelEmployeeDeviceVO;
import com.lesso.lzh.sunflower.service.IRelEmployeeDeviceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 员工与设备关系 前端控制器
 * </p>
 *
 * @author zqz
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/rel/employee-device")
public class RelEmployeeDeviceController {
    @DubboReference
    private IRelEmployeeDeviceService relEmployeeDeviceService;

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody RelEmployeeDeviceDTO dto) {
        try {
            return ApiResponse.<Long>ok().data(relEmployeeDeviceService.save(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Long>error().msg("保存员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/page")
    public ApiResponse<Page<RelEmployeeDeviceVO>> page(RelEmployeeDeviceQueryDTO dto) {
        try {
            return ApiResponse.<Page<RelEmployeeDeviceVO>>ok()
                    .data(relEmployeeDeviceService.page(dto))
                    .build();
        } catch (Exception e) {
            return ApiResponse.<Page<RelEmployeeDeviceVO>>error().msg("分页查询员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<RelEmployeeDeviceVO>> customPage(RelEmployeeDeviceQueryDTO dto) {
        try {
            return ApiResponse.<Page<RelEmployeeDeviceVO>>ok().data(relEmployeeDeviceService.customPage(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Page<RelEmployeeDeviceVO>>error().msg("自定义分页查询员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        try {
            relEmployeeDeviceService.deleteById(id);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("删除员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @GetMapping
    public ApiResponse<Object> selectById(@RequestParam("id") Long id) {
        try {
            Object result = relEmployeeDeviceService.selectById(id);
            return ApiResponse.ok().data(result).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("查询员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @PutMapping
    public ApiResponse<Object> update(@RequestBody RelEmployeeDeviceDTO dto) {
        try {
            relEmployeeDeviceService.update(dto);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("更新员工设备关系失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/list-by-employee")
    public ApiResponse<Object> listByEmployeeId(
            @RequestParam("employeeId") Long employeeId) {
        try {
            return ApiResponse.ok().data(relEmployeeDeviceService.listByEmployeeId(employeeId)).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("根据员工ID查询设备关系失败: " + e.getMessage()).build();
        }
    }
}